package com.example.txjc.Activity;

import android.app.Activity;
import android.app.Application;
import android.content.Context;
import android.content.SharedPreferences;

import com.tencent.bugly.Bugly;


import org.litepal.LitePalApplication;
import org.xutils.x;

import java.util.LinkedList;
import java.util.List;

/**
 * Created by Administrator on 2018/1/20.
 * 自定义application
 */
public class MyApplication extends Application {
    private SharedPreferences sharedPreferences;
    private SharedPreferences.Editor editor;
    private static MyApplication instance;
    private List<Activity> activitys = null;
   // public static DisplayImageOptions mNormalImageOptions;
   // public AppPreferences appPreferences;
    public MyApplication() {
        super();
        this.activitys = new LinkedList<Activity>();
    }

    @Override
    public void onCreate() {
        super.onCreate();
        //TypefaceProvider.registerDefaultIconSets();
        LitePalApplication.initialize(this);
        x.Ext.init(this);
        x.Ext.setDebug(true);
        //initImageLoader(this);
        Bugly.init(getApplicationContext(), "d614bdb584", true);
    }

    // 添加Activity到容器中
    public void addActivity(Activity activity) {
        if (activitys != null && activitys.size() > 0) {
            if(!activitys.contains(activity)){
                activitys.add(activity);
            }
        }else{
            activitys.add(activity);
        }

    }

    // 遍历所有Activity并finish
    public void exit() {
        if (activitys != null && activitys.size() > 0) {
            for (Activity activity : activitys) {
                activity.finish();
            }
        }
        //System.exit(0);
    }
    public static MyApplication getInstance() {
        if (null == instance) {
            instance = new MyApplication();
        }
        return instance;

    }

   /* public void saveUser(String uname, String nickname, String userid, String areaid, String areaname, String level){
        appPreferences = new AppPreferences(getApplicationContext());
        appPreferences.put("username", uname);
        appPreferences.put("nickname", nickname);
        appPreferences.put("userid", userid);
        appPreferences.put("areaid", areaid);
        appPreferences.put("areaname", areaname);
        appPreferences.put("level", level);
    }*/

    public void setSharedPreference(String uname, String nickname, String userid, String areaid, String areaname, String level) {
        sharedPreferences = getSharedPreferences("user", Context.MODE_PRIVATE);
        editor = sharedPreferences.edit();
        editor.putString("username", uname);
        editor.putString("nickname", nickname);
        editor.putString("userid",userid);
        editor.putString("areaid",areaid);
        editor.putString("areaname",areaname);
        editor.putString("level",level);
       // editor.commit();// 提交修改
        editor.apply();
    }
    public void setSharedPreference2(String name, String phone, String character) {
        sharedPreferences = getSharedPreferences("message", Context.MODE_PRIVATE);
        editor = sharedPreferences.edit();
        editor.putString("username", name);
        editor.putString("phone", phone);
        editor.putString("character",character);
        editor.commit();// 提交修改
    }
   /* public static void initImageLoader(Context context) {
        int memoryCacheSize = (int) (Runtime.getRuntime().maxMemory() / 5);
        MemoryCache memoryCache;
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.GINGERBREAD) {
            memoryCache = new LruMemoryCache(memoryCacheSize);
        } else {
            memoryCache = new LRULimitedMemoryCache(memoryCacheSize);
        }

        mNormalImageOptions = new DisplayImageOptions.Builder().bitmapConfig(Bitmap.Config.RGB_565).cacheInMemory(true).cacheOnDisc(true)
                .resetViewBeforeLoading(true).build();
        ImageLoaderConfiguration config = new ImageLoaderConfiguration.Builder(context).defaultDisplayImageOptions(mNormalImageOptions)

                .memoryCache(memoryCache)
                // .memoryCacheSize(memoryCacheSize)
                .tasksProcessingOrder(QueueProcessingType.LIFO).threadPriority(Thread.NORM_PRIORITY - 2).threadPoolSize(3).build();

        // Initialize ImageLoader with configuration.
        ImageLoader.getInstance().init(config);
    }*/
}
